/*
 * Copyright 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package simple.runtime.components.impl.android;

import simple.runtime.android.MainActivity;
import simple.runtime.components.表格布局;
import simple.runtime.components.impl.android.util.ViewUtil;
import simple.runtime.components.可视组件;

import android.content.Context;
import android.view.ViewGroup;
import android.widget.TableRow;
import android.widget.TableLayout;

/**
 * 一个允许将子组件放置在表格窗体中的布局组件。
 * 
 * @author David Foster
 * @author Herbert Czymontek
 * @author 东子 xhwsd@qq.com
 */
public class 表格布局Impl extends 布局Impl implements 表格布局 {

  // 表格布局的行和列数
  private int rows;
  private int columns;

  // 指示表大小无法再更改
  private boolean fixed;

  /**
   * 创建一个新的表格布局。
   *
   * @param container  视图容器
   */
  表格布局Impl(视图组件容器 container) {
    // TableLayout https://developer.android.google.cn/reference/android/widget/TableLayout
    super(new TableLayout(MainActivity.getContext()), container);

    TableLayout layoutManager = (TableLayout) getLayoutManager();
    layoutManager.setStretchAllColumns(true);
  }

  /**
   * {@inheritDoc}
   *
   * Note that table layout uses {@link #placeComponent(视图组件)} to
   * actually add the component. This is necessary because you need to set both
   * the {@link 可视组件#列()} and {@link 可视组件#行()}
   * properties before the component can be put into the layout.
   */
  @Override
  public void addComponent(视图组件 component) {
  }

  /**
   * 将组件放入其列和行中。
   *
   * @param component  准备好放入其列和行中的组件
   */
  public void placeComponent(视图组件 component) {
    ensureTableInitialized();

    int column = component.列();
    int row = component.行();
    if (0 <= column && column < columns && 0 <= row && row < rows) {
      ViewGroup layoutManager = getLayoutManager();
      ((TableRow) layoutManager.getChildAt(row)).addView(component.getView(),
          new TableRow.LayoutParams(column));
    }
  }

  private void ensureTableInitialized() {
    if (!fixed) {
      // 添加第一个组件后，无法再调整表的大小
      fixed = true;

      ViewGroup layoutManager = getLayoutManager();
      Context context = layoutManager.getContext();
      for (int row = 0; row < rows; row++) {
        layoutManager.addView(new TableRow(context), new TableLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
      }
    }
  }

  /**
   * 设置子组件相当于布局的对齐方式
   *
   * @param component 组件
   * @param justification 对齐
   */
  public void setComponentJustification(视图组件 component, int justification) {
    // TableLayout.LayoutParams
    // https://developer.android.google.cn/reference/android/widget/TableLayout.LayoutParams
    ((TableLayout.LayoutParams) component.getView().getLayoutParams()).gravity =
        ViewUtil.simpleToAndroidGravity(justification);
    component.getView().requestLayout(); 
  }

  // 表格布局 属性实现

  @Override
  public void 行数(int newRows) {
    rows = newRows;
  }

  @Override
  public void 列数(int newColumns) {
    columns = newColumns;
  }
}
